﻿$iframeObj.getTempAppData = {};
$iframeObj.setIframeScrollTop = function () {
    parent.$HASH['iframeScrollTop'] = document.documentElement.scrollTop || document.body.scrollTop
};
$iframeObj.replaceApp = function (el, callBack) {
    var newEl = el.replaceWith($('#tempAppContent').children());
    $('#tempAppContent').empty();
    appLoaded();
    resizeScroll();
    if ($.isFunction(callBack)) {
        setTimeout(callBack, 1000)
    }
};
$iframeObj.getApp = function (url, data, callback) {
    $.getJSON(url, data,
    function (backData) {
        if (data.type == 'layout') {
            var el = $(' <div class="row"  role="dragEl" path="row"><div class="rowPatch">' + backData.data.html + '</div></div>');
            $('>div >div', el).each(function () {
                $(this).append('<div class="boxBlock"><div class="elementContainer" path="column">' + parent.getEmptyEl() + '</div></div>')
            });
            $('#tempAppContent').append(el)
        } else {
            $('#tempAppContent').append(backData.data.html)
        };
        callback(backData)
    })
};
$iframeObj.reloadResponsiveImage = function () {
    $('img.responsiveImage').responsiveImage(1)
};
$iframeObj.setDraggable = function () {
    $('#elToolsPanel').draggable({
        handle: '>div.dressTitle',
        start: function (event, ui) {
            $('<div class="appMask" id="pageMask" style="position:fixed;"></div>').appendTo('body');
            parent.appDraggableStart(event, ui)
        },
        drag: function (event, ui) {
            parent.appDraggableDrag(event, ui)
        },
        stop: function (event, ui) {
            parent.appDraggableStop(event, ui);
            $('#pageMask').remove()
        }
    })
};
$iframeObj.disableDraggable = function () {
    $('#elToolsPanel').draggable("disable")
};
$iframeObj.enableDraggable = function () {
    $('#elToolsPanel').draggable("enable").css('opacity', 1)
};
$iframeObj.setCKEDITOR = function (id, type) {
    $('#' + id).data('editorId', id);
    $('#' + id).attr('contenteditable', true).find('img.responsiveImage').removeClass('responsiveImage');
    var opts = {
        on: {
            change: function () {
                resizeScroll()
            }
        }
    };
    if (type == 'article') {
        opts = $.extend(true, opts, {
            extraPlugins: 'image,table,horizontalrule,format,font,colorbutton,colordialog,maximize,justify,contextmenu',
            toolbarGroups: [{
                name: 'clipboard',
                groups: ['clipboard', 'undo']
            },
            {
                name: 'editing',
                groups: ['find', 'selection', 'spellchecker']
            },
            {
                name: 'forms'
            },
            {
                name: 'basicstyles',
                groups: ['basicstyles', 'cleanup']
            },
            {
                name: 'paragraph',
                groups: ['list', 'align']
            },
            {
                name: 'links'
            },
            {
                name: 'insert'
            },
            {
                name: 'styles'
            },
            {
                name: 'colors'
            },
            {
                name: 'tools'
            },
            {
                name: 'others'
            },
            ],
            removeButtons: 'Cut,Copy,Paste,Anchor,Subscript,Superscript'
        })
    };
    CKEDITOR.inline(id, opts);
    $('#' + id).trigger('focus')
};
$iframeObj.destroyCKEDITOR = function (id) {
    var vid = $('#' + id).data('editorId');
    if (CKEDITOR.instances[vid]) {
        CKEDITOR.instances[vid].destroy();
        $('#' + vid).removeClass('cke_focus').removeAttr('contenteditable').find('img').each(function () {
            var src = $(this).attr('src').replace('scale_768', 's').replace('scale_480', 's').replace('scale_1440', 's');
            $(this).addClass('responsiveImage').css('height', 'auto').attr('data-src', src)
        })
    }
};